var a = 0; //统计正确次数
var b = 0; //统计错误次数
var c = 0; //统计总个数
var aa = 1000;
var bb = 40;
window.onkeydown = function(e) {
	var key = e.keyCode;
	var top = document.getElementById("top");
	var tongji = document.getElementById("tongji");
	for (var i = 0; i < top.children.length; i++) {
		var code = top.children[i].getAttribute("keycode");
		if (key == code) {
			top.removeChild(top.children[i]);
			a++;
			tongji.innerHTML = a;
			break;
		}
	}
	lanzi();
}
var id = 0;
var id1 = 0;
var id2;

function kaishi() {
	if (id == 0 && id1 == 0) {

		id = setInterval("chuangjianPG()", aa) //出的个数
			//间隔时间创建苹果
		id1 = setInterval("xialuo()", bb)
			//控制苹果移动
		jishi();
	}

}

function zanting() {
	clearInterval(id);
	clearInterval(id1);
	clearInterval(id2);
	id = 0;
	id1 = 0;
}

function jieshu() {
	var top = document.getElementById("top");
	var y = document.getElementById("shijian");
	var tongji = document.getElementById("tongji");
	var zong = document.getElementById("zong");
	var cuowu = document.getElementById("cuowu");
	var zql = document.getElementById("zhengquelv");
	var lanzi = document.getElementById("lanzi");
	top.innerHTML = ""
	y.innerHTML = ""
	x = 0;
	z = 0;
	q = 0;
	a = 0;
	b = 0;
	c = 0;
	lanzi.innerHTML = "";
	tongji.innerHTML = "";
	zong.innerHTML = "";
	cuowu.innerHTML = "";
	zql.innerHTML = "";
	clearInterval(id);
	clearInterval(id1);
	clearInterval(id2);
	id = 0;
	id1 = 0;

}
var x = 0;
var z = 0;
var q = 0;

function jishi() {
	var y = document.getElementById("shijian") 
	id2 = setInterval(function() {
		x++;
		if (x >= 60) {
			q = x % 60;
			z = Math.floor(x / 60);
			y.innerHTML = z + "分" + q + "秒"
		} else {
			y.innerHTML = x + "秒";
		}
	}, 1000)

}

function chuangjianPG() {
	var zong = document.getElementById("zong")
	var div = document.createElement("div"); //创建一个苹果的div
	var num = parseInt(Math.random() * 26) + 65; //A-Z
	div.innerHTML = "&#" + num + ";"; //随机给苹果一个字母
	div.setAttribute("keycode", num); //把num存到新建属性key里面
	var left = parseInt(Math.random() * 700);
	div.style.left = left + "px";
	div.style.top = "0px";
	document.getElementById("top").appendChild(div)
	c++;
	zong.innerHTML = c;
	var zql = document.getElementById("zhengquelv");
	zql.innerHTML = parseInt(a / c * 100) + "%"
}

function xialuo() {
	var top = document.getElementById("top");
	var cuowu = document.getElementById("cuowu");
	for (var i = 0; i < top.children.length; i++) {
		var t = parseInt(top.children[i].style.top);
		t += 1; //控制下落快慢
		top.children[i].style.top = t + "px";
		if (t > 380) {
			top.children[i].innerHTML = ""
			top.children[i].className = "psPG"
		}
		if (t > 400) {
			top.removeChild(top.children[i]);
			b++;
			cuowu.innerHTML = b;
			if (b == 10) {
				alert("闯关失败！");
				jieshu();
			}
		}
	}
}

function lanzi() {
	var lanzi = document.getElementById("lanzi");
	var div = document.createElement("div");
	if (a == 10) {
		div.className = "lanzi-pg";
		lanzi.appendChild(div);
	} else if (a == 20) {
		div.className = "lanzi-pg1";
		lanzi.appendChild(div);
	} else if (a == 30) {
		div.className = "lanzi-pg2";
		lanzi.appendChild(div);
	} else if (a == 40) {
		div.className = "lanzi-pg3";
		lanzi.appendChild(div);
	} else if (a == 50) {
		div.className = "lanzi-pg4";
		lanzi.appendChild(div);
		alert("恭喜闯关成功！");
		jieshu();
	}
}

function shezhi() {
	document.getElementById("shezhi").style.display = "block"
}

function quxiao() {
	document.getElementById("shezhi").style.display = "none"
}

function queding() {
	var dengji = document.getElementById("dengji").selectedIndex;
	(function ceshi(d) {
		switch (d) {
			case 0:
				aa = 1000;
				bb = 40;
				break;
			case 1:
				aa = 800;
				bb = 30;
				break;
			case 2:
				aa = 600;
				bb = 20;
				break;
			case 3:
				aa = 400;
				bb = 10;
				break;
		}
		kaishi();
		quxiao();
	})(dengji);
}

function moren() {
	document.getElementById("dengji").selectedIndex = 0;
	document.getElementById("tureshuliang").selectedIndex = 4;
	document.getElementById("cuoshuliang").selectedIndex = 0;
}

function tuichu() {
	document.getElementById("main").style.display = "none";
}